package com.wirelesshomecontrol;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.rapplogic.xbee.api.ApiId;
import com.rapplogic.xbee.api.RemoteAtRequest;
import com.rapplogic.xbee.api.RemoteAtResponse;
import com.rapplogic.xbee.api.XBee;
import com.rapplogic.xbee.api.XBeeAddress;
import com.rapplogic.xbee.api.XBeeAddress16;
import com.rapplogic.xbee.api.XBeeAddress64;
import com.rapplogic.xbee.api.XBeeException;
import com.rapplogic.xbee.api.XBeeRequest;
import com.rapplogic.xbee.api.XBeeResponse;
import com.rapplogic.xbee.api.XBeeTimeoutException;
import com.rapplogic.xbee.api.wpan.IoSample;
import com.rapplogic.xbee.api.wpan.RxResponseIoSample;
import com.rapplogic.xbee.examples.wpan.IoSamplesExample;

public class TranscieverControl {
	
	public boolean changeStatus(XBee xbee, String COMport) throws XBeeException {
		XBeeRequest request;
		XBeeRequest request1;
		xbee.open(COMport, 9600);			
		
		XBeeAddress64 addr64 = new XBeeAddress64(0, 0, 0, 0, 0, 0, 0, 0);
		
		//Check the status of D2. If ON then turn off D1, turn on D3
		if(getD5Status(xbee)){
			request = new RemoteAtRequest(addr64, "D1", new int[] {4});
			request1 = new RemoteAtRequest(addr64, "D3", new int[] {5});
		//else turn ON D1 and turn off D3
		}else {
			request = new RemoteAtRequest(addr64, "D1", new int[] {5});
			request1 = new RemoteAtRequest(addr64, "D3", new int[] {4});
		}

		RemoteAtResponse response = (RemoteAtResponse) xbee.sendSynchronous(request, 10000);
		RemoteAtResponse response1 = (RemoteAtResponse) xbee.sendSynchronous(request1, 10000);
		
		if (response.isOk() && response1.isOk()) {
			//log.info("successfully turned on pin 20 (D0)");
			return true;
		} else {
			throw new RuntimeException("failed to turn on pin 20.  status is " + response.getStatus());
		} 
	}
	
	//Check to see if D1 is on by checking D2
	public boolean getD2Status(XBee xbee) throws XBeeException{
		XBeeResponse response = xbee.getResponse();
		RxResponseIoSample ioSample = (RxResponseIoSample)response;
		boolean D2 = false;
		
		for(IoSample sample: ioSample.getSamples()){
			D2 = sample.isD2On();
			System.out.println("Digital D2 is " + (sample.isD2On() ? "on" : "off"));
		}
		
		return D2;
	}
	
	//Check to see if D3 is on by checking D4
	public boolean getD4Status(XBee xbee) throws XBeeException{
		XBeeResponse response = xbee.getResponse();
		RxResponseIoSample ioSample = (RxResponseIoSample)response;
		boolean D4 = false;
		
		for(IoSample sample: ioSample.getSamples()){
			D4 = sample.isD4On();
			System.out.println("Digital D4 is " + (sample.isD4On() ? "on" : "off"));
		}
		
		return D4;
	}
	
	public boolean getD5Status(XBee xbee) throws XBeeException{
		XBeeResponse response = xbee.getResponse();
		RxResponseIoSample ioSample = (RxResponseIoSample)response;
		boolean D5 = false;
		
		for(IoSample sample: ioSample.getSamples()){
			D5 = sample.isD5On();
			System.out.println("Digital D5 is " + (sample.isD5On() ? "on" : "off"));
		}
		
		return D5;
	}
	
	//Checks the current sensor to see if the light is on or off
	public boolean getInputStatus(XBee xbee) throws XBeeException{
		XBeeResponse response = xbee.getResponse();
		RxResponseIoSample ioSample = (RxResponseIoSample)response;
		boolean R5 = false;
		
		for(IoSample sample: ioSample.getSamples()){
			R5 = sample.isD5On();
			System.out.println("Digital D5 is " + (sample.isD5On() ? "on" : "off"));
		}
		
		return R5;
	}
	
	public boolean getInputAnalog(XBee xbee, String COMport, XBeeAddress address64) throws XBeeException {
		
		xbee.open(COMport, 9600);
        System.out.println("Is the Xbee connected? " + xbee.isConnected());
        XBeeResponse response = xbee.getResponse(10000);
		
        if(response.getApiId() == ApiId.RX_16_IO_RESPONSE || response.getApiId() == ApiId.RX_64_IO_RESPONSE) {
        	RxResponseIoSample ioSample = (RxResponseIoSample) response;
        	
        	System.out.println("Recieved a sample from " + ioSample.getSourceAddress());
        	System.out.println("RSSI is " + ioSample.getRssi());
        	
        	for (IoSample sample: ioSample.getSamples()) {
        		System.out.println(sample.getAnalog5());
        	}
        }
		xbee.close();
		return true;
	}
	
	 public void XBeeOff(XBeeAddress64 Address) throws XBeeException, InterruptedException{
	    	System.out.println(Address + " off");
	    	XBee xbee = new XBee();
			
			try {
				// replace with your coordinator com/baud
				xbee.open("COM6", 9600);
				// xbee.open("COM5", 9600);			
				
				// replace with SH + SL of your end device
				XBeeAddress64 addr64 = (Address);
				
				
				RemoteAtRequest requestD3 = new RemoteAtRequest(addr64, "D3", new int[] {4});
				RemoteAtRequest requestD1 = new RemoteAtRequest(addr64, "D1", new int[] {5});
				
				RemoteAtResponse response1 = (RemoteAtResponse) xbee.sendSynchronous(requestD1, 10000);
				RemoteAtResponse response2 = (RemoteAtResponse) xbee.sendSynchronous(requestD3, 10000);
				
				
				
				if (response1.isOk() && response2.isOk()) {
					//log.info("successfully turned on pin 20 (D0)");	
				} else {
					throw new RuntimeException("failed to turn on pin 20.  status is " + response1.getStatus());
				} 
				
			} catch (XBeeTimeoutException e) {
				//log.error("request timed out. make sure you remote XBee is configured and powered on");
			} catch (Exception e) {
				//log.error("unexpected error", e);
			} finally {
				xbee.close();
			}
	    }
	    
	public void XBeeOn(XBeeAddress64 Address) throws XBeeException, InterruptedException{
		System.out.println(Address + " on");
	    	XBee xbee = new XBee();
			
	    	try {
				// replace with your coordinator com/baud
				xbee.open("COM6", 9600);
				// xbee.open("COM5", 9600);			
				
				// replace with SH + SL of your end device
				XBeeAddress64 addr64 = (Address);
				
				// turn on end device (pin 20) D0 (Digital output high = 5) 
				//RemoteAtRequest request = new RemoteAtRequest(addr64, "D0", new int[] {5});
				//RemoteAtRequest request = new RemoteAtRequest(addr64, "IR", new int[] {0x7f, 0xff});
				//RemoteAtRequest request = new RemoteAtRequest(addr64, "D5", new int[] {3});
				//RemoteAtRequest request = new RemoteAtRequest(addr64, "D0", new int[] {2});
				RemoteAtRequest requestD3 = new RemoteAtRequest(addr64, "D3", new int[] {5});
				RemoteAtRequest requestD1 = new RemoteAtRequest(addr64, "D1", new int[] {4});
				
				RemoteAtResponse response1 = (RemoteAtResponse) xbee.sendSynchronous(requestD1, 10000);
				RemoteAtResponse response2 = (RemoteAtResponse) xbee.sendSynchronous(requestD3, 10000);
				
				
				
				/*// wait a bit
				Thread.sleep(5000);
//				
//				// now turn off end device D0
				requestD1.setValue(new int[] {5});//Off =4, On =5
				requestD3.setValue(new int[] {5});
				
				response1 = (RemoteAtResponse) xbee.sendSynchronous(requestD1, 10000);
				response2 = (RemoteAtResponse) xbee.sendSynchronous(requestD3, 10000);*/
				
				if (response1.isOk() && response2.isOk()) {
					//log.info("successfully turned on pin 20 (D0)");	
				} else {
					throw new RuntimeException("failed to turn on pin 20.  status is " + response1.getStatus());
				} 
				
			} catch (XBeeTimeoutException e) {
				//log.error("request timed out. make sure you remote XBee is configured and powered on");
			} catch (Exception e) {
				//log.error("unexpected error", e);
			} finally {
				xbee.close();
			}
	    }
	
}


